package com.sky.controller.admin;

import cn.hutool.core.util.IdUtil;
import com.sky.result.Result;
import com.sky.utils.AliOssUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * PROJECT_NAME: tlias-test
 * NAME: UploadController
 *
 * @AUTHOR: zhang
 * DATE: 2023/11/8
 * DESCRIPTION :
 */
@RestController
@Slf4j
@RequestMapping("/admin/common")
public class UploadController {
    @Resource
    private AliOssUtil aliOssUtil;

    @PostMapping("/upload")
    public Result upload(MultipartFile file) throws IOException {
        log.info("【开始上传图片】");
        //获取文件的原始名称
        String originalFilename = file.getOriginalFilename();
        //获取到文件的后缀
        String suffixFileName = originalFilename.substring(originalFilename.lastIndexOf("."));
        //重新生成一个不重复的文件名  concat:用于将指定的字符串参数连接到字符串上。
        String buckedFileName = IdUtil.fastSimpleUUID().concat(suffixFileName);
        //基于阿里云上传工具上传原始文件并接收访问URL路径
        String imageURL = aliOssUtil.upload(file.getBytes(), buckedFileName);
        return Result.success(imageURL);
    }
}
